package com.fjph.myapplication.ui.adapter

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import com.bumptech.glide.Glide
import com.fjph.myapplication.R
import com.fjph.myapplication.model.StoriesBean
import com.fjph.myapplication.ui.activity.WebViewActivity
import com.fjph.myapplication.ui.adapter.ZhihuAdapter.ViewHolder


import java.util.ArrayList

class ZhihuAdapter(datas: List<StoriesBean>, internal var mActivity: Activity) : RecyclerView.Adapter<ZhihuAdapter.ViewHolder>() {
    internal var datas: List<StoriesBean> = ArrayList()

    init {
        this.datas = datas
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.zhihu_item, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {

        val data = datas[position]
        viewHolder.mTextView.text = data.title

        Glide.with(mActivity)
                .load(data.images!![0])
                .into(viewHolder.img)

        viewHolder.itemView.setOnClickListener {
            val intent = Intent(mActivity, WebViewActivity::class.java)
            intent.putExtra("stories", data.id)
            mActivity.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var mTextView: TextView
        var img: ImageView

        init {
            mTextView = view.findViewById<View>(R.id.txt_title) as TextView
            img = view.findViewById<View>(R.id.img) as ImageView
        }
    }

}